/*
	Copyright (C) 2011 Daniel C. Dennison (dandennison84@gmail.com).  All rights reserved.
	This file is licensed under the EPL 1.0 (Eclipse Public License 1.0)
	A copy of the license can be found at http://www.eclipse.org/legal/epl-v10.html
*/

package com.gmail.dandennison84.accounting

import org.junit.Before
import org.junit.Test

class CashFlowStatementTest {
	List<Account> coa
	Map<String, List<Account>> incomeStatement
	JournalHelper jh

	@Before
	void before() {
		coa = new ChartOfAccountsHelper().chartOfAccounts
		jh = new JournalHelper()
		incomeStatement = jh.getIncomeStatement(coa)
	}

	@Test
	void testCashFlowStatment() {
		StatementBuilder reb = new StatementBuilder(coa)
		reb.retainedEarnings {
			equityAccounts(accountType:AccountType.EQUITY)
		}

		Map<String, List<Account>> retainedEarningsStatement = reb.accounts

		double netIncome = jh.getNetIncome(incomeStatement)
		double equity = retainedEarningsStatement['equityAccounts'].inject(0) {
			acc, it -> acc + (it.creditBalance - it.debitBalance)
		}
		assert 1000 == equity
		assert 1010 == equity + netIncome

		StatementBuilder cfs = new StatementBuilder(coa)
		cfs.cashFlowStatement {
			cashAccounts(cashFlowStatementType:CashFlowStatementType.CASH_ACCOUNT)
			operatingActivities(cashFlowStatementType:CashFlowStatementType.OPERATING_ACTIVITIES)
			investingActivities(cashFlowStatementType:CashFlowStatementType.INVESTING_ACTIVITIES)
			financingActivities(cashFlowStatementType:CashFlowStatementType.FINANCING_ACTIVITIES)
		}
		Map<String, List<Account>> cashFlowStatement = cfs.accounts
		assert 4 == cashFlowStatement.size()
		assert 1 == cashFlowStatement['cashAccounts'].size()
		assert 21 == cashFlowStatement['operatingActivities'].size()
		assert 8 == cashFlowStatement['investingActivities'].size()
		assert 5 == cashFlowStatement['financingActivities'].size()

		/*
		Cash: 			$510
		Investments: 		$100
		Inventory:		$450
		Accounts Payable		  $50
		Common Stock			$1000
		Freight Revenues		 $100
		Cost of Sales		 $50
		Interest Income			  $10
		Advertising Expense	 $50

		Net Income:				   $10
		Change in Inventory:			 ($450)
		Change in Accounts Payable:		   $50
		-------
		Cash from Operations			 ($390)

		Change in Investments			 ($100)

		Change in Financing			 $1000

		Net Increase in Cash:			  $510
		*/

		double cash = cashFlowStatement['cashAccounts'].inject(0) {
			acc, it -> acc + (it.debitBalance - it.creditBalance)
		}
		double oa = cashFlowStatement['operatingActivities'].inject(0) {
			acc, it -> acc + (it.creditBalance - it.debitBalance)
		}
		double ia = cashFlowStatement['investingActivities'].inject(0) {
			acc, it -> acc + (it.creditBalance - it.debitBalance)
		}
		double fa = cashFlowStatement['financingActivities'].inject(0) {
			acc, it -> acc + (it.creditBalance - it.debitBalance)
		}

		assert cash == oa + ia + fa
	}
} 
